﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nauka.engine;

namespace Nauka.GUI
{
    public partial class DBManagementWindow : Form
    {
        private MainProgram mp;
        public DBManagementWindow(MainProgram mp)
        {
            this.mp = mp;
            InitializeComponent();
        }

        private void DBManagementWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Visible = false;
        }

        private void DBManagementWindow_Load(object sender, EventArgs e)
        {
            serverNameTextBox.Text = mp.dbconn.dbSettings.serverName;
            userTextBox.Text = mp.dbconn.dbSettings.userName;
            passwordTextBox.Text = mp.dbconn.dbSettings.password;
            passwordTextBox.PasswordChar = '*';
            dataBaseTextBox.Text = mp.dbconn.dbSettings.dbName;
        }

        private void ConnectionCancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void ConnectionOkButton_Click(object sender, EventArgs e)
        {
            mp.dbconn.dbSettings.serverName = serverNameTextBox.Text;
            mp.dbconn.dbSettings.userName = userTextBox.Text;
            mp.dbconn.dbSettings.password = passwordTextBox.Text;
            mp.dbconn.dbSettings.dbName = dataBaseTextBox.Text;
            mp.dbconn.dbSettings.buildConnectionString();
            try
            {
                mp.dbconn.tryConnect();
                this.Dispose();
            }
            catch(Exception exc){
                MessageBox.Show("Cannot connect to DataBase \n" + exc.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
            }
            
        }

    }
}
